package com.sinochem.yunlian.ship.base.biz;

import com.sinochem.yunlian.ship.api.vo.uc.UcUserInfo;
import com.sinochem.yunlian.ship.base.entity.BsAppBanner;
import com.sinochem.yunlian.ship.base.mapper.BsAppBannerMapper;
import com.sinochem.yunlian.ship.base.vo.request.BsBannerVo;
import com.sinochem.yunlian.ship.common.biz.BaseBiz;
import com.sinochem.yunlian.ship.common.enums.ResponseCode;
import com.sinochem.yunlian.ship.common.msg.RestResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Created by ${pc} on 2018/1/27
 */
@Service
public class BsAppBannerBiz extends BaseBiz<BsAppBannerMapper,BsAppBanner> {

    @Autowired
    private BsAppBannerMapper bsAppBannerMapper;

    public List<BsAppBanner> getBannerList(){
        return bsAppBannerMapper.findBannerList();
    }

    public void addBanner(BsAppBanner banner){
        Integer seqNum = 1;
        BsAppBanner maxBanner = this.getMaxBanner(banner);
        if(maxBanner!=null){
            seqNum = maxBanner.getSeqNum()+1;
        }
        banner.setSeqNum(seqNum);
        banner.setCreateTime(new Date());
        bsAppBannerMapper.insertSelective(banner);
    }

    public void updateBanner(BsAppBanner banner){
        banner.setUpdateTime(new Date());
        bsAppBannerMapper.updateByPrimaryKeySelective(banner);
    }


    /**
     * 逻辑删除
     * @param banner
     */
    public void deleteBanner(BsAppBanner banner){
        //bsAppBannerMapper.updateByPrimaryKey(banner);
        BsAppBanner updateBanner = new BsAppBanner();
        updateBanner.setId(banner.getId());
        updateBanner.setDeleteStatus(1);
        updateBanner.setUpdateBy(banner.getUpdateBy());
        this.updateBanner(updateBanner);
    }

    public void deleteBanner(BsBannerVo bannerVo){
        //bsAppBannerMapper.updateByPrimaryKey(banner);
        UcUserInfo ucUserInfo = bannerVo.getUcUserInfo();
        BsAppBanner updateBanner = new BsAppBanner();
        updateBanner.setId(bannerVo.getId());
        updateBanner.setDeleteStatus(1);
        updateBanner.setUpdateBy(ucUserInfo.getId());
        this.updateBanner(updateBanner);
    }
    public List<BsAppBanner> getBannerList(BsBannerVo bannerVo){
        Example example = new Example(BsAppBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if (bannerVo.getType()!=null) {
            criteria.andEqualTo("type", bannerVo.getType());
        }
        if (StringUtils.isNotBlank(bannerVo.getTitle())) {
            criteria.andLike("title", "%"+bannerVo.getTitle()+"%");
        }
        criteria.andEqualTo("deleteStatus",0);
        example.setOrderByClause("seq_num");
        return bsAppBannerMapper.selectByExample(example);
    }

    /**
     * banner 移动
     * @param bannerVo
     * @return
     */
    @Transactional(rollbackFor =  Exception.class)
    public RestResponse moveBanner(BsBannerVo bannerVo){
        // 要移动的banner
        BsAppBanner banner = bsAppBannerMapper.selectByPrimaryKey(bannerVo.getId());
        if(banner==null || banner.getId()==null){
            return new RestResponse(ResponseCode.INVALID_PARAM);
        }
        BsAppBanner moveDbanner = null;//被移动的banner
        if("UP".equalsIgnoreCase(bannerVo.getDirectionFlag())){
            if((moveDbanner =  this.moveBannerUp(banner))==null)
                return new RestResponse(ResponseCode.CANNOT_MOVE_UP);
        }else if("DOWN".equalsIgnoreCase(bannerVo.getDirectionFlag())){
            if((moveDbanner =  this.moveBannerDown(banner))==null)
                return new RestResponse(ResponseCode.CANNOT_MOVE_DOWN);
        }

        UcUserInfo ucUserInfo = bannerVo.getUcUserInfo();
        //交换顺序
        BsAppBanner updateBanner = new BsAppBanner();
        updateBanner.setId(banner.getId());
        updateBanner.setSeqNum(moveDbanner.getSeqNum());
        updateBanner.setUpdateBy(ucUserInfo.getId());

        BsAppBanner updateMovedBanner = new BsAppBanner();
        updateMovedBanner.setId(moveDbanner.getId());
        updateMovedBanner.setSeqNum(banner.getSeqNum());
        updateMovedBanner.setUpdateBy(ucUserInfo.getId());

        this.updateBanner(updateBanner);
        this.updateBanner(updateMovedBanner);

        return RestResponse.success();
    }

    /**
     * 上移相邻
     * @param banner
     * @return
     */
    private BsAppBanner moveBannerUp(BsAppBanner banner){

        Example example = new Example(BsAppBanner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLessThan("seqNum",banner.getSeqNum())
                .andEqualTo("type", banner.getType())
                .andEqualTo("deleteStatus",0);
        example.setOrderByClause("seq_num desc");
        List<BsAppBanner>  appBannerList = bsAppBannerMapper.selectByExample(example);
        //第一条不能上移
        if(appBannerList==null || appBannerList.size()==0){
            return null;
        }
        return appBannerList.get(0);//上一条banner
    }

    /**
     * 下移相邻
     * @param banner
     * @return
     */
    private BsAppBanner moveBannerDown(BsAppBanner banner){
        Example example = new Example(BsAppBanner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThan("seqNum",banner.getSeqNum())
                .andEqualTo("type", banner.getType())
                .andEqualTo("deleteStatus",0);
        example.setOrderByClause("seq_num");
        List<BsAppBanner>  appBannerList = bsAppBannerMapper.selectByExample(example);
        //最后一条不能下移
        if(appBannerList==null || appBannerList.size()==0){
            return null;
        }
        return appBannerList.get(0);//下一条banner
    }

    /**
     * 获取某一类的最大序号行
     * @param banner
     * @return
     */
    private BsAppBanner getMaxBanner(BsAppBanner banner){
        Example example = new Example(BsAppBanner.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("type", banner.getType());
        example.setOrderByClause("seq_num desc");
        List<BsAppBanner>  appBannerList = bsAppBannerMapper.selectByExample(example);
        if(appBannerList==null || appBannerList.size()==0){
            return null;
        }
        return appBannerList.get(0);//seq_num 最大的一条
    }
}